﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;

namespace FindDuplicateFile
{
    /// <summary>
    /// Noeud de type Fichier
    /// </summary>
    public class FSFileNode : FSNode
    {
        public FSFileNode(FileInfo finfo)
        {
            Name = finfo.Name;
            Path = finfo.DirectoryName;
            Size = finfo.Length;
            CreationTime = finfo.CreationTime;
            LastAccessTime = finfo.LastAccessTime;
            LastWriteTime = finfo.LastWriteTime;
            MD5Hash = null;
        }

        public override void OpenDirectory()
        {
            ProcessStartInfo fileFolder = new ProcessStartInfo("explorer.exe", String.Format("/select,\"{0}\\{1}\"", Path,Name));
            fileFolder.UseShellExecute = false;
            Process.Start(fileFolder);
        }

        public override void OpenFile()
        {
            Process.Start(Path+"\\"+Name);
        }

        public override void Delete()
        {
            File.Delete(Path);
        }

        public void ComputeFileMD5Hash()
        {
            string fileName = Path + "\\" + Name;
            using (var stream = new BufferedStream(File.OpenRead(fileName), 1200000))
            {
                SHA256Managed sha = new SHA256Managed();
                byte[] checksum = sha.ComputeHash(stream);
                MD5Hash = BitConverter.ToString(checksum).Replace("-", string.Empty);
            }
        }

        public override string ToString()
        {
            return string.Format("{0},{1}", Name, Size.ToString());
        }

        static public void ExportExcel(List<FSFileNode> lstFiles)
        {
            //if (this.dataGrid1.ItemsSource == null) return;

            //List<FileAttrib> outputRows = this.dataGrid1.ItemsSource as List<FileAttrib>;

            //object oOpt = System.Reflection.Missing.Value; //for optional arguments
            //Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
            //Microsoft.Office.Interop.Excel.Workbooks oWBs = oXL.Workbooks;
            //Microsoft.Office.Interop.Excel._Workbook oWB = oWBs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //Microsoft.Office.Interop.Excel._Worksheet oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets[1];

            //int numberOfRows = outputRows.Count;
            //Microsoft.Office.Interop.Excel.Range oRange;

            //oRange = oSheet.get_Range("A1", "A1");
            //oRange.Value2 = "Nom du fichier";
            //oRange = oSheet.get_Range("B1", "B1");
            //oRange.Value2 = "Chemin";
            //oRange = oSheet.get_Range("C1", "C1");
            //oRange.Value2 = "Taille";
            //oRange = oSheet.get_Range("D1", "D1");
            //oRange.Value2 = "Creation Time";
            //oRange = oSheet.get_Range("E1", "E1");
            //oRange.Value2 = "Last Access Time";
            //oRange = oSheet.get_Range("F1", "F1");
            //oRange.Value2 = "Last Write Time";
            //oRange = oSheet.get_Range("F1", "F1");
            //oRange.Value2 = "Groupe";

            //int row = 2;
            //foreach (FileAttrib fa in outputRows)
            //{
            //    oRange = oSheet.get_Range("A"+row.ToString(), "A"+row.ToString());
            //    oRange.Value2 = fa.Name;
            //    oRange = oSheet.get_Range("B"+row.ToString(), "B"+row.ToString());
            //    oRange.Value2 = fa.Path;
            //    oRange = oSheet.get_Range("C"+row.ToString(), "C"+row.ToString());
            //    oRange.Value2 = fa.Size;
            //    oRange = oSheet.get_Range("D"+row.ToString(), "D"+row.ToString());
            //    oRange.Value2 = fa.CreationTime.ToString();
            //    oRange = oSheet.get_Range("E"+row.ToString(), "E"+row.ToString());
            //    oRange.Value2 = fa.LastAccessTime.ToString();
            //    oRange = oSheet.get_Range("F"+row.ToString(), "F"+row.ToString());
            //    oRange.Value2 = fa.LastWriteTime.ToString();
            //    oRange = oSheet.get_Range("G" + row.ToString(), "G" + row.ToString());
            //    oRange.Value2 = fa.fileImpression;

            //    row++;
            //}

            //oXL.Visible = true;
            //oXL.UserControl = true;
        }

        #region --------------- Property --------------------------
        public string MD5Hash { get { return m_MD5Hash; } set { if (value != m_MD5Hash) { m_MD5Hash = value; NotifyPropertyChanged("MD5Hash"); } } }
        private string m_MD5Hash;
        #endregion --------------- Property --------------------------
    }
}
